package jaoso.framework.service.impl;

import jaoso.framework.dao.AccountDAO;
import jaoso.framework.dao.RoleDAO;

import jaoso.framework.domain.Account;
import jaoso.framework.domain.Manager;
import jaoso.framework.domain.Right;
import jaoso.framework.domain.Role;

import jaoso.framework.exception.DAOException;

import jaoso.framework.security.Acl;
import jaoso.framework.security.ProtectedResource;

import jaoso.framework.service.SetupService;

import jaoso.framework.util.MyUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.orm.hibernate.LocalSessionFactoryBean;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author 边缘孤客 edgeloner@yahoo.com.cn
 * @since 2005-1-6
 */
public class SetupServiceImpl implements SetupService
{
    private static Log log = LogFactory.getLog(SetupServiceImpl.class);

    private AccountDAO accountDAO;

    private Acl acl;

    private LocalSessionFactoryBean lsfb;

    private RoleDAO groupDAO;

    public void setAccountDAO(AccountDAO accountDAO)
    {
        this.accountDAO = accountDAO;
    }

    public void setAcl(Acl acl)
    {
        this.acl = acl;
    }

    public void setGroupDAO(RoleDAO groupDAO)
    {
        this.groupDAO = groupDAO;
    }

    public void setLsfb(LocalSessionFactoryBean lsfb)
    {
        this.lsfb = lsfb;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.service.SetupService#install()
     */
    public void install()
    {
        if (isNotCreateDatabaseScheme())
        {
            createDatabaseScheme();
            createRoleNright();
        }
    }

    private LocalSessionFactoryBean getLsfb()
    {
        return lsfb;
    }

    private boolean isNotCreateDatabaseScheme()
    {
        try
        {
            groupDAO.findAllGroup();
        } catch (DAOException e)
        {
            return true;
        }

        return false;
    }

    private void createDatabaseScheme()
    {
        getLsfb().createDatabaseSchema();
    }

    private void createRoleNright()
    {
        Role group = new Role();
        group.setDesc("Root");

        Role tmpGroup = new Role();
        tmpGroup.setDesc("TEMP");

        Manager person = new Manager();
        person.setName("root");

        Date date = new Date();
        person.setCreatedate(date);
        person.setLastdate(date);

        Account user = new Account();
        user.setName("root");
        user.setPassword(MyUtils.toMD5("root"));
        user.setGroup(group);
        //user.setManager(person);

        Set rights = new HashSet();
        Set groups = new HashSet();
        groups.add(group);

        ProtectedResource item;

        for (Iterator it = acl.getProtectedResources().iterator(); it.hasNext();)
        {
            item = (ProtectedResource) it.next();

            Right right = new Right();
            right.setBox(item.getBox());
            right.setDesc(item.getDesc());
            right.setUrl(item.getUrl());
            right.setGroups(groups);
            rights.add(right);
            log.info("a right " + right);
        }

        group.setRights(rights);

        try
        {
            accountDAO.createAccount(user);
            groupDAO.createGroup(tmpGroup);
        } catch (DAOException e)
        {
            log.error("init Right error: " + e);
        }
    }
}